package core.profile.dao;

import core.util.HibernateCoreUtil;
import core.profile.model.BasicCredential;
import core.profile.model.BasicProfile;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import frontend.registration.controller.Encript;
import frontend.user.controller.GetDataByEmailId;

public class RegisterDao {

public static boolean registerUser(BasicCredential bc,BasicProfile bp) {

                   SessionFactory sf = HibernateCoreUtil.getSessionFactory();

                 Transaction t = null;

         try {

                       Session s = sf.openSession();

                      t = s.beginTransaction(); // start a new transaction
                      

                     s.persist(bc);
                     s.persist(bp);

                      t.commit(); // commit transaction

                         return true;

              } catch (Exception ex) {

          System.err.println("Error -->" + ex.getMessage());

           if (t != null)

                    t.rollback();

                   return false;

         }

}


public boolean getUser(String userId,String password)
{
	
	boolean validation = false;
	
	
	Session session = HibernateCoreUtil.getSessionFactory().openSession();
	Transaction transaction = null;
	
	try 
	{
			//from Category c where c.name='"+name+"' and c.path='"+path+"'")
		BasicCredential bc = GetDataByEmailId.getBasicCredentialByUserId(userId);
		if(bc == null)
		{
			validation=false;
		}
		else
		{
			String encriptPass = Encript.doEncript(password);
			if(encriptPass.equals(bc.getPassword()))
			{
				validation=true;
			}
		}
		
	}
	catch (HibernateException e) 
	{
		// TODO: handle exception
		transaction.rollback();
		e.printStackTrace();
	}
	finally 
	{
		session.close();
	}
	return validation;
}



}